package com.idanchuang.count.controller;

import com.idanchuang.count.service.SimpleCountService;
import com.idanchuang.count.service.api.client.SimpleCountClient;
import com.idanchuang.count.service.api.request.command.CountBatchResetCommand;
import com.idanchuang.count.service.api.request.query.CountBatchQuery;
import com.idanchuang.count.service.api.request.query.CountQuery;
import com.idanchuang.component.base.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Map;

/**
 * @author Richard_yyf
 * @version 1.0 2021/9/18
 */
@Slf4j
@RestController
public class SimpleCountController implements SimpleCountClient {

    @Resource
    private ParamsChecker paramsChecker;

    @Resource
    private SimpleCountService simpleCountService;

    @Override
    public JsonResult<Long> query(@Valid CountQuery countQuery) {
        return JsonResult.success(simpleCountService.query(countQuery));
    }

    @Override
    public JsonResult<Map<String, Long>> batchQuery(@Valid CountBatchQuery countBatchQuery) {

        paramsChecker.checkQueryBatchSize(countBatchQuery.getBizNos().size());

        return JsonResult.success(simpleCountService.batchQuery(countBatchQuery));
    }

    @Override
    public JsonResult<Void> batchResetCount(@Valid CountBatchResetCommand countBatchResetCommand) {

        paramsChecker.checkResetBatchSize(countBatchResetCommand.getItems().size());

        simpleCountService.batchResetCount(countBatchResetCommand);

        return JsonResult.OK;
    }
}
